var wechatMessageData = [];

$(document).ready(function() {
	showLoading();
	setBaseUrl();
	$('title', parent.document).html($('title').text());
	createTableHead("wechatMessageThead", wechatMessageTable, search);
    initPage();
})
function addKeyWord(){
    var length = $(".addKey").length + 4;
    var addey = '';
    addey = '<div class="col-sm-12" id="keyDiv'+ length +'">' +
    '<label class="col-sm-2 control-label partConditionTitle marginBottom2Ten">模版信息'+ length +':</label>' +
    '<label for="keyWord'+ length +'Value" class="col-sm-1 control-label partConditionTitle marginBottom2Ten">value</label>' +
    ' <div class="col-sm-7 partCondition">' +
    '<input  type="text" class="form-control addKey" id="keyWord'+ length +'Value" name="keyWord'+ length +'Value" value=""/>' +
    '</div>' +
    '<div class="col-sm-1" style="color:red; font-size: 35px;width: 40px;">*</div>' +
    '<div class="col-sm-1 spanKey" id="spanKey'+ length +'"><span class="glyphicon glyphicon-remove" onclick="delKey('+ length +')"></span></div>' +
    '</div>';
    $("#addKeyWord").append(addey);
    if ($(".addKey").length > 0) {
        $(".spanKey").hide();
        $("#spanKey" + length).show();
    }
}
function delKey(num){
    $("#keyDiv" + num).remove();
}
function checkGetID() {
    var checks = document.getElementsByName("getID");
    for (var i=0;i<checks.length;i++) {
        if (checks[i].checked == true) {
            return true;
        }
    }
    return false;
}
function showSendMessage(){
    $ajaxList('getWechatUserName', getUserID);
    var checks = document.getElementsByName("getID");
    for(i=0;i<checks.length;i++){
        checks[i].checked = false;
    }
    $("#sendMessageDiv").show();
}
function getUserID(data){
    $("#sendUserIDcheckBox").empty();
    var length = data.length;
    for (var i = 0;i < length; i++){
        if (data && data[i] && data[i].userID && data[i].userName) {
            $("#sendUserIDcheckBox").append('<label style="width: 110px;margin-right: 5px;">' +
                '<input name="getID" id="sID' + i + '" type="checkbox" value="' + data[i].userID + '"/> ' + data[i].userName + '</label>');
        }
    }
}
function hideSendMessage(){
    $("#sendMessageDiv").hide();
}
function sendMessage(){
    showLoading();
    var getID = '';
    var checks = document.getElementsByName("getID");
    if (checkGetID() == false) {
        showMessage("请先选择发送用户");
        setTimeout(hideLoading, 500);
        return;
    }
    for(i=0;i<checks.length;i++){
        if(checks[i].checked) {
            getID += "'" +document.getElementsByName("getID")[i].value + "',";
        }
    }
    $("#sendUserID").val(getID.substring(0,getID.length-1));
    for (var i=0; i < $(".addKey").length; i++) {
        var keyID = "keyWord" + (i+4) +"Value";
        var keyName = "模版信息" + (i+4);
        wechatKeyWordTable.push({headerNameEN: keyID, headerNameCN: keyName, allowEmpty: false});
    }
    if (!checkForm("keyForm", wechatKeyWordTable)) {
        setTimeout(hideLoading, 500);
        return;
    }
    var keyData = '';
    keyData = '{"first":{"value":"'+ $("#firstValue").val() +'","color":"#FF0000"},' +
        '"keyword1":{"value":"'+ $("#keyWord1Value").val() +'","color":"#0000FF"},' +
        '"keyword2":{"value":"'+ $("#keyWord2Value").val() +'"},'+
        '"keyword3":{"value":"'+ $("#keyWord3Value").val() +'"},';
    for (var i=0; i < $(".addKey").length; i++) {
        keyData +=  '"keyword'+ (i+4) +'":{"value": "'+ $("#keyWord"+ (i+4) +"Value").val() +'"},';
    }
    keyData +=  '"remark":{"value":"'+ $("#remarkValue").val() +'","color": "#B7B7B7"}}';

    var jsondata = {
        sendFrom: $("#sendFrom").val(),
        templateID: $("#TemplateID").val(),
        url: $("#Url").val(),
        appid : $("#appid").val(),
        pagepath : $("#pagepath").val(),
        data: keyData,
        userID: getID.substring(0,getID.length-1)
    };
	$ajaxCommon('sendWeChatMessage', sendMessageResult, JSON.stringify(jsondata));
}
function sendMessageResult(data){
    setTimeout(hideLoading, 500);
    var result = data.result;
    if (result.successUser || result.failUser) {
        var message = result.successUser ? "微信消息发送成功用户：<br>" + result.successUser: "";
        if (message) {
            message += result.failUser ? "<br>微信消息发送失败用户：<br>" + result.failUser : "";
        } else {
            message = result.failUser ? "微信消息发送失败用户：<br>" + result.failUser : "";
        }
        showMessage(message);
    } else if (result == "TemplateIDEmpty") {
        showMessage("微信消息发送失败：消息模板为空");
    } else if (result == "UserIDEmpty") {
        showMessage("微信消息发送失败：发送用户为空");
    } else if (result == "DataEmpty") {
        showMessage("微信消息发送失败：发送消息内容为空");
    } else if (result == "TokenError") {
        showMessage("微信消息发送失败：获取微信token失败");
    } else if (result == "NotExistUser") {
        showMessage("微信消息发送失败：发送用户不存在");
    } else if (result == "Empty") {
        showMessage("微信消息发送失败：发送数据为空");
    }
    search();
}
function search(){
	$ajax('searchWechatMessage', {
        pageNum : $("#currentPage").val(),
        pageSize: $("#rows").val(),
    }, searchWechatMessage);
}
function searchWechatMessage(data){
    setPage(data.total, $("#rows").val());
    wechatMessageData = data && data.messagelist || [];
	createTableBody("wechatMessageTbody", wechatMessageTable, wechatMessageData);
}
